import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.*;
import org.apache.flink.types.Row;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class ProcessDataForDruid {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        String topic = "stage7_module2_data";
        SimpleStringSchema simpleStringSchema = new SimpleStringSchema();
        Properties props = new Properties();
        props.setProperty("bootstrap.servers","linux101:9092");
        props.setProperty("group.id","druid");

        FlinkKafkaConsumer<String> consumer = new FlinkKafkaConsumer<>(topic, simpleStringSchema, props);
        consumer.setStartFromEarliest();
        consumer.setCommitOffsetsOnCheckpoints(false);

        DataStreamSource<String> data = env.addSource(consumer);
        SingleOutputStreamOperator<String> mapped = data.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String value, Collector<String> out) throws Exception {
                JSONObject jsonObject = JSON.parseObject(value);
                JSONArray products = jsonObject.getJSONArray("products");
                jsonObject.remove("products");
                for (Object element : products) {
                    JSONObject e = (JSONObject) element;
                    jsonObject.put("product", e);
//                    long productId = e.getLong("productId");
//                    String productName = e.getString("productName");
//                    double price = e.getDouble("price");
//                    int productNum = e.getInteger("productNum");
//                    long categoryid = e.getLong("categoryid");
//                    String catname1 = e.getString("catname1");
//                    String catname2 = e.getString("catname2");
//                    String catname3 = e.getString("catname3");
                    out.collect(jsonObject.toJSONString());
                    jsonObject.remove("product");
                }
            }
        });

        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "linux101:9092");

        FlinkKafkaProducer<String> myProducer = new FlinkKafkaProducer<String>(
                "druid",
                new SimpleStringSchema(),
                properties);

        mapped.addSink(myProducer);

        env.execute();
    }
}
